
<%@ page import="gfo.Bracket; gfo.TournamentController; gfo.Matchship; gfo.Match; gfo.Player; gfo.Tournament" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <g:set var="entityName" value="${message(code: 'tournament.label', default: 'Tournament')}" />
        <title><g:message code="default.show.label" args="[entityName]" /></title>
    </head>
    <body>
        <div class="body">
            <h1><g:message code="default.show.label" args="[entityName]" />
                <span class="menuButton"><g:link class="create" action="create">New Tournament</g:link></span></h1>
            <g:if test="${flash.message}">
            <div class="message">${flash.message}</div>
            </g:if>

            <div class="about"><h1>${tournamentInstance.name}</h1>

                <tr class="prop">
                    <td valign="top" class="name">Description</td>
                    <td valign="top" class="value">
                        ${tournamentInstance.description}
                    </td>
                </tr>
            <p>The ${tournamentInstance.name} was the second-to-last Brawl tournament in the
            George Foreman Open Series of the 2009-2010 school year. It was won by
            ${Player.findByFullName("Kyle Lewis")}, who went entirely undefeated. The
                ${tournamentInstance.name} was notable in that entrants were only allowed to play
            as a particular character once, forcing everyone to play at eight different characters.</p></div>

            <!--Okay, this is the fun part. In Round Robin, every player faces each other one time, and
            this can be effectively visualized as a grid. The following logic basically says that for the
            entry for the ith row and jth column, provide a link to the specific match between those two
            players. If i and j are the same, leave the cell blank (a player never faces himself, obviously).
            The table is incomplete for a number of reasons, for instance that it skips the first player in
            the collection, but the core functionality is here.-->
            <div class="results">
                <h2>Round Robin:</h2>
                <table class="results">
                    <tbody>
                    <g:each in="${0..tournamentInstance.roster().size()}" status="i" var="playerNum">
                        <tr class="${i==0? 'value':'result'}">
                            <g:each in="${0..tournamentInstance.roster().size()}" status="j" var="oppNum">
                                <td class="${j==0? 'value':'result'}">
                                    <g:if test="${i!=j}">
                                        <g:if test="${i==0}">
                                            <g:link controller="player" action="show" id="${tournamentInstance.roster()[j-1].id}">
                                                ${tournamentInstance.roster()[j-1].fullName}
                                            </g:link>
                                        </g:if>
                                        <g:elseif test="${j==0}">
                                            <g:link controller="player" action="show" id="${tournamentInstance.roster()[i-1].id}">
                                                ${tournamentInstance.roster()[i-1].fullName}
                                            </g:link>
                                        </g:elseif>
                                        <g:else test="${i!=0}">
                                            <g:link controller="match" action="show"
                                                    id="${tournamentInstance.findMatchByPlayers(Player.findById(tournamentInstance.roster()[i-1].id),
                                                            Player.findById(tournamentInstance.roster()[j-1].id))?.id}">
                                                ${tournamentInstance.rrResult(Player.findById(tournamentInstance.roster()[i-1].id),
                                                        Player.findById(tournamentInstance.roster()[j-1].id))}
                                            </g:link>
                                        </g:else>
                                    </g:if>
                                </td>
                            </g:each>
                            <g:if test="${i==0}">
                                <td class="result">Total</td>
                            </g:if>
                            <g:else>
                                <td class="resTotal">${tournamentInstance.rrRecord(tournamentInstance.roster()[i-1])}</td>
                            </g:else>
                        </tr>
                    </g:each>
                    </tbody>
                </table>
                <!--This is where we use Tournament.hasBracketMatches. If the particular tournament
                has no bracket matches, there is no need to show any of this stuff. Otherwise, I attempt
                to setup the data in a visually interesting way--although clearly I haven't quite gotten
                there.-->
                <g:if test="${tournamentInstance.hasBracketMatches()}">
                    <h2>Bracket:</h2>
                    <g:each in="${Bracket.list()}" var="bracket">
                        <g:each in="${gfo.Match.findAllByTournamentAndBracket(tournamentInstance,bracket)}" var="match">
                            <g:link controller="match" action="show" id="${match.id}">${match}</g:link>
                        </g:each>
                    </g:each>
                </g:if>
            </div>
    </body>
</html>
